﻿/*  ----------------------------------------------------------------------------
 *  Microsoft Public License (http://www.microsoft.com/opensource/licenses.mspx#Ms-PL)
 *  
 *  ----------------------------------------------------------------------------
 *  DynamicAspect
 *  ----------------------------------------------------------------------------
 *  File:       ValidatorAspect.cs
 *  Author:     NiceWk (n9walker@hotmail.com)
 *  ----------------------------------------------------------------------------
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Bizall.Dynamic.Aspect;

namespace BankSample
{
    class ValidatorAspect : AspectBase
    {
        public override void OnBeforeMethodCall(WeavingContext context)
        {

            var methodName = context.InvokeMemberBinder.Name;
            if (methodName == "Withdraw" || methodName == "Deposit")
            {
                var amount = (decimal)context.ArgumentValues[0];
                if (amount <= 0)
                    throw new Exception("Amount should be greater than 0");

                if (methodName == "Withdraw" && context.Target is Bank)
                {
                    var bank = (Bank)context.Target;
                    if (amount > bank.Account)
                        throw new Exception("Amount is greater than current account.");
                }
            }

        }
    }
}